package top.kaoshanji.r5ch12c;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/5/9 22:25
 */
@Service("userService")
public class UserService extends BaseService {

    private JdbcTemplate jdbcTemplate;

    private ScoreService scoreService;

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Autowired
    public void setScoreService(ScoreService scoreService) {
        this.scoreService = scoreService;
    }

    public void updateLastLogonTime(String userName) {
        String sql = " UPDATE t_user1 u SET u.last_logon_time = ? WHERE user_name =? ";
        jdbcTemplate.update(sql, System.currentTimeMillis(), userName);
    }

    // 代码清单-r5ch12c-402页a：嵌套调用了本类的其他方法及其他服务类的方法
    public void logon(String userName) {

        System.out.println("...运行于本服务updateLastLogonTime方法之前...");
        updateLastLogonTime(userName);
        System.out.println("...运行于本服务updateLastLogonTime方法之后...");

        System.out.println("...运行于其他服务addScore方法之前...");
        scoreService.addScore(userName, 20);
        System.out.println("...运行于其他服务addScore方法之后...");

    }

}
